package mashuaiproject.mashuaiproject.serivce;

import mashuaiproject.mashuaiproject.dto.AddFriendsUser;
import mashuaiproject.mashuaiproject.dto.GuanzhuUsersDto;
import mashuaiproject.mashuaiproject.dto.OtherMesDto;
import mashuaiproject.mashuaiproject.dto.SaveUsersQuesionDato;
import mashuaiproject.mashuaiproject.mapper.*;
import mashuaiproject.mashuaiproject.model.*;
import org.apache.catalina.User;
import org.apache.ibatis.annotations.Insert;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GuanZhuUserSerivce {
    @Autowired
    private AttributeMappper attributeMappper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SaveQuesionMapper saveQuesionMapper;
    @Autowired
    private QuesionMapper quesionMapper;
    @Autowired
    private OtherMapper otherMapper;
    @Autowired
    private  OnlieCoummtiyMapper onlieCoummtiyMapper;
    public List<GuanzhuUsersDto> selectGuanzhuUserMes(Users users,Integer page,Integer size,String tags) {
        //查找出该用户的粉丝信息
        List<GuanzhuUsersDto> guanzhuUsersDtoList=new ArrayList<>();//存放信息
        //查找出当前用户的总收藏信息
        Integer countPage=0;//总页数
        Integer offset=(page-1)*size;//第几条显示的条数
        Integer totulout=attributeMappper.selectFSCount(users.getId());
        if(totulout%size==0){
            countPage=totulout/size;
        }else if(totulout%size!=0){
            countPage=totulout/size+1;
        }

        List<GuanZhu> guanZhuList=attributeMappper.selectOneGuanzhu(users.getId(),offset,size);
        if(guanZhuList!=null){
            for(GuanZhu guanZhu:guanZhuList){
                //根据粉丝id查找出对应信息
                Users users1=userMapper.findByUser(guanZhu.getUser_id());
                GuanzhuUsersDto guanzhuUsersDto=new GuanzhuUsersDto();
                BeanUtils.copyProperties(guanZhu,guanzhuUsersDto);
                guanzhuUsersDto.setUsers(users1);
                guanzhuUsersDto.setPage(page);
                guanzhuUsersDto.setCountPage(countPage);
                guanzhuUsersDtoList.add(guanzhuUsersDto);
            }
        }

      //  System.err.println(guanzhuUsersDtoList);
        return guanzhuUsersDtoList;
    }

    public List<SaveUsersQuesionDato> selectSaveUsersQuesionDatoes(Integer id,Integer page,Integer size,String tags) {
        List<SaveUsersQuesionDato> saveUsersQuesionDatoList=new ArrayList<>();
        //查找出当前用户的总收藏信息
        Integer countPage=0;//总页数
        Integer offset=(page-1)*size;//第几条显示的条数
        Integer totulout=saveQuesionMapper.selectTotul(id);//总数据
        if(totulout%size==0){
            countPage=totulout/size;
        }else if(totulout%size!=0){
            countPage=totulout/size+1;
        }
        List<Save> saveList=saveQuesionMapper.selectOneSave(id,offset,size);
        if (saveList!=null){
            for(Save save:saveList){
                //找出收藏文章的博主
                Users users=userMapper.findByUser(save.getFather_id());
                Quesions quesions=quesionMapper.getByidQuesion(save.getQuesion_id());
                SaveUsersQuesionDato saveUsersQuesionDato=new SaveUsersQuesionDato();
                BeanUtils.copyProperties(save,saveUsersQuesionDato);
                saveUsersQuesionDato.setUsers(users);
                saveUsersQuesionDato.setQuesions(quesions);
                //将页数添加进去
                saveUsersQuesionDato.setCountPage(countPage);
                saveUsersQuesionDato.setPage(page);
                saveUsersQuesionDatoList.add(saveUsersQuesionDato);
            }
        }


        return saveUsersQuesionDatoList;
    }

    public int delSaves(Integer id, Integer del) {
        int delSaves=saveQuesionMapper.delSaves(id,del);//删除文章
        //文章收藏数减去1
        int updatSaves=quesionMapper.updateSaves(del);
        return 1;
    }

    public List<GuanzhuUsersDto> selectMyGuanzhu(Integer id, Integer page, Integer size) {
        //查找出该用户的粉丝信息
        List<GuanzhuUsersDto> MyGuanzhu=new ArrayList<>();//存放信息
        //查找出当前用户的总收藏信息
        Integer countPage=0;//总页数
        Integer offset=(page-1)*size;//第几条显示的条数
        Integer totulout=attributeMappper.selectMyGuanzhu(id);
        if(totulout%size==0){
            countPage=totulout/size;
        }else if(totulout%size!=0){
            countPage=totulout/size+1;
        }

        List<GuanZhu> guanZhuList=attributeMappper.selectOneMyGuanzhu(id,offset,size);
        if(guanZhuList!=null){
            for(GuanZhu guanZhu:guanZhuList){
                //根据粉丝id查找出对应信息
                Users users1=userMapper.findByUser(guanZhu.getBozhu_id());

                GuanzhuUsersDto guanzhuUsersDto=new GuanzhuUsersDto();
                BeanUtils.copyProperties(guanZhu,guanzhuUsersDto);
                guanzhuUsersDto.setUsers(users1);
                guanzhuUsersDto.setPage(page);
                guanzhuUsersDto.setCountPage(countPage);
                MyGuanzhu.add(guanzhuUsersDto);
            }
        }
       // System.err.println(MyGuanzhu);
        return MyGuanzhu;
    }

    public int delMyGuanzhus(Integer id, Integer delMyGuanzhu) {
        int del=attributeMappper.delGuanzhu(id,delMyGuanzhu);
        //用户粉丝减去1
        int delFensi=userMapper.delFenSi(delMyGuanzhu);
        return 1;
    }

    public List<OtherMesDto> selectAllXunZhang(Integer id) {
        //查找当前自己的勋章
        List<OtherMesDto> otherMesDtoArrayList=new ArrayList<>();
        List<SelfXunzhang> selfXunzhangs=otherMapper.selectOneXunzhang(id);
        List<XunZhang> xunZhangList=otherMapper.selectAllXunZhang();//查找到所用胸章
        for(XunZhang xunZhang:xunZhangList){
            OtherMesDto otherMesDto=new OtherMesDto();
            boolean isfind=false;//用于记录
            for (int i=0;i<selfXunzhangs.size();i++){
                if(selfXunzhangs.get(i).getXunzhang_id()==xunZhang.getId()){
                    Boolean isGetXunzhang=true;//表示已经获取到这个勋章
                    otherMesDto.setIsGetXunzhang(isGetXunzhang);
                    otherMesDto.setXunZhang(xunZhang);
                    otherMesDtoArrayList.add(otherMesDto);
                    isfind=true;
                    break;
                }else {
                    isfind=false;
                }

            }
            //说明没有用户没有这个勋章
            if(!isfind){
                Boolean isGetXunzhang=false;//表示已经获取到这个勋章
                otherMesDto.setIsGetXunzhang(isGetXunzhang);
                otherMesDto.setXunZhang(xunZhang);
                otherMesDtoArrayList.add(otherMesDto);
            }
        }

        //判断是否有符合条件的胸章
        return otherMesDtoArrayList;
    }

    public Map<String, Object> selectAllmes(Integer xunzhangId, String isGetXunzhang, Integer id) {
        Map<String ,Object> map=new HashMap<>();
        XunZhang xunZhang=otherMapper.selectJudegXUnzhng(xunzhangId);//找出勋章信息

        //判断多少人领取
        Integer getCount=otherMapper.selectGetOneCoung(xunzhangId);
        //根据勋章介绍查找不同区别的胸章
        //判断勋章是否已经获取
        String  btnName="";//是否领取
        String  jindu="";
        String  tishiMes="";//提示信息
        boolean isHasXunzhang=false;//根据条件判断后默认不可以领取
        if(isGetXunzhang.equals("true")){
            //给按钮赋值
            btnName="已领取";
            jindu="100";
            tishiMes="你已经完成任务";
            isHasXunzhang=true;
        }else {
             btnName="未领取";
             //判断当前还有多少
            if(xunZhang.getName().equals("勤写标兵")){
                //判断是否有首次发布问题
                tishiMes="发布一篇文章即可获取";
                jindu="0";
            }
            if(xunZhang.getName().equals("三好学生")){
                Integer commentCount=otherMapper.selecCommentCOunt(id);
                if(commentCount>=10){
                    isHasXunzhang=true;
                }else {
                    isHasXunzhang=false;
                }
                //算出百分比
                NumberFormat numberFormat = NumberFormat.getInstance();
                numberFormat.setMaximumFractionDigits(2);
                jindu = numberFormat.format((float) commentCount / (float) 10 * 100);
                tishiMes="距离获取该勋章还差："+commentCount+"/10个评论";
            }
            if(xunZhang.getName().equals("收藏达人")){
                Integer saveCOunt=otherMapper.selectSaveNoSelf(id);
                if(saveCOunt>=20){
                    isHasXunzhang=true;
                }else {
                    isHasXunzhang=false;
                }
                NumberFormat numberFormat = NumberFormat.getInstance();
                numberFormat.setMaximumFractionDigits(2);
                jindu = numberFormat.format((float) saveCOunt / (float) 20 * 100);
                tishiMes="距离获取该勋章还需要收藏："+saveCOunt+"/20个文章";
            }
            if(xunZhang.getName().equals("翰林学士")){
              jindu="0";
              tishiMes="排行榜还在开发中，请等待";
            }
            if(xunZhang.getName().equals("勤写能手")){
                Integer quesionCOunt=otherMapper.selectFabuQUeison(id);
                if(quesionCOunt>=5){
                    isHasXunzhang=true;
                }else {
                    isHasXunzhang=false;
                }
                NumberFormat numberFormat = NumberFormat.getInstance();
                numberFormat.setMaximumFractionDigits(2);
                jindu = numberFormat.format((float) quesionCOunt / (float) 5 * 100);
                tishiMes="距离获取该勋章还需要发布："+quesionCOunt+"/5个文章";

            }
            if(xunZhang.getName().equals("知深远博")){
                List<Quesions> quesionsList = quesionMapper.selectOneQueisons(id);
                Integer viecount=0;
                for (Quesions quesions:quesionsList){
                    viecount+=quesions.getView_count();
                }
                if(viecount>=100){
                    isHasXunzhang=true;
                }else {
                    isHasXunzhang=false;
                }
                NumberFormat numberFormat = NumberFormat.getInstance();
                numberFormat.setMaximumFractionDigits(2);
                jindu = numberFormat.format((float) viecount / (float) 100 * 100);
                tishiMes="距离获取该勋章还差："+viecount+"/100浏览量";
            }
            if(xunZhang.getName().equals("粉丝达人")){
                Integer fensiCount=otherMapper.selectFensiCount(id);
                if(fensiCount>=10){
                    isHasXunzhang=true;
                }else {
                    isHasXunzhang=false;
                }
                NumberFormat numberFormat = NumberFormat.getInstance();
                numberFormat.setMaximumFractionDigits(2);
                jindu = numberFormat.format((float) fensiCount / (float) 10 * 100);
                tishiMes="距离获取该勋章还差："+fensiCount+"/10个粉丝";
            }
            if(xunZhang.getName().equals("社区专家")){
                jindu ="0%";
                tishiMes="条件满足时候找站长申请";
            }
        }

        //需要的put信息
        map.put("xunZhang",xunZhang);//当前勋章
        map.put("getCount",getCount);//多少人获取
        map.put("btnName",btnName);//是否已经领取
        map.put("jindu",jindu);
        map.put("tishiMes",tishiMes);
        map.put("isHasXunzhang",isHasXunzhang);
        return map;

    }

    public Integer insertXZ(Integer id, Integer xzId) {
        //查找该勋章的信息
        Integer inser=otherMapper.insertXZ(id,xzId);
        //增加200积分：
        Integer updateJifen=otherMapper.updateJifen(id,200);
        //增加20经验
        Integer updateExp=otherMapper.updateExp(id,20);
        //增加2经验
        Integer updateWeiwang=otherMapper.updateExp(id,2);

        return inser;
    }

    public List<Dongtai> selectAllDongtai(Integer id) {
        //找出当前用户前二十条动态
        //余下的删除
        List<Dongtai> dongtaiList=otherMapper.selectOneDongTai(id);
        //如果超过二十条就删除多余的
        for(int i=0;i<dongtaiList.size();i++){
            if(i>=20){
                //将前二十天信息保留，余下的删除
                Integer delDontai=otherMapper.delDongtai(dongtaiList.get(i).getId());
            }
        }

        List<Dongtai> dongtaiList2=otherMapper.selectOneDongTai(id);
        return dongtaiList2;
    }

    public List<AddFriendsUser> selectOneFriedn(Integer id) {
        //根据id查找所有的好友申请
        List<AddFriend> addFriendList=onlieCoummtiyMapper.selectOneFriend(id);
        List<AddFriendsUser> addFriendsUserList=new ArrayList<>();
        //绑定用户的好友信息
        if(addFriendList.size()!=0){
            for(AddFriend addFriend:addFriendList){
                Users users=userMapper.findByUser(addFriend.getUserId());
                AddFriendsUser addFriendsUser=new AddFriendsUser();
                addFriendsUser.setAddFriend(addFriend);
                addFriendsUser.setUsers(users);
                addFriendsUserList.add(addFriendsUser);
            }
        }

        return addFriendsUserList;
    }

}
